package com.example.online_order.service.impl;

import com.example.online_order.Eception.BussinessException;
import com.example.online_order.Eception.ExceptionType;
import com.example.online_order.mapper.AdminAccountMapper;
import com.example.online_order.mapper.ClientAccountMapper;
import com.example.online_order.pojo.AdminAccount;
import com.example.online_order.pojo.ClientAccount;
import com.example.online_order.service.ClientAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true)
public class ClientAccoutServiceImpl implements ClientAccountService {
    @Autowired
    ClientAccountMapper mapper;

    @Override
    public ClientAccount login(ClientAccount acc) {
        String username = acc.getUserName();
        ClientAccount account = mapper.findClientByUsername(username);
        // 是否存在用户
        if (account == null) {
            throw  new BussinessException(ExceptionType.bus_invalid_username,"用户名错误");
        }else{
            if (acc.getPassword().equals(account.getPassword()))
                return account;
            else {
                throw new BussinessException(ExceptionType.bus_invalid_password,"密码错误");
            }
        }
    }

    @Transactional
    @Override
    public Integer addClient(ClientAccount clientAccount) {
        ClientAccount accountCl = mapper.findClientByUsername(clientAccount.getUserName());
        if (accountCl != null){
            throw new BussinessException(ExceptionType.bus_id_exists,"用户名已存在");
        }else {
            clientAccount.setIdentify("cl");
            Integer res = mapper.addclient(clientAccount);
            if (res != 1){
                throw new BussinessException(ExceptionType.bus_insert_fail,"添加顾客失败");
            }else {
                return res;
            }
        }

    }


    @Transactional
    @Override
    public Integer deleteClient(Integer id) {
        ClientAccount accountCl = mapper.findClientById(id);
        if (accountCl == null){
            throw new BussinessException(ExceptionType.bus_id_not_found,"用户不存在");
        }else {
            Integer res = mapper.deleteClient(id);
            if (res!= 1){
                throw new BussinessException(ExceptionType.bus_delete_fail,"删除顾客失败");
            }else {
                return res;
            }
        }

    }


    @Transactional
    @Override
    public Integer updateClient(ClientAccount clientAccount) {
        ClientAccount accountCl = mapper.findClientById(clientAccount.getId());
        if (accountCl == null){
            throw new BussinessException(ExceptionType.bus_id_not_found,"用户不存在");
        }else {
            Integer res = mapper.updateClient(clientAccount);
            if (res!= 1){
                throw new BussinessException(ExceptionType.bus_update_fail,"修改顾客失败");
            }else {
                return res;
            }
        }

    }

    @Override
    public List<ClientAccount> getAllClient() {
        return mapper.getAllClient();
    }

    @Override
    public ClientAccount findClientByUsername(String username) {
        ClientAccount accountCl = mapper.findClientByUsername(username);
        if(accountCl == null) {
            throw new BussinessException(ExceptionType.bus_id_not_found,"用户不存在");
        }else{
            return accountCl;
        }
    }

    @Override
    public ClientAccount findClientLikeUsername(String username) {
        ClientAccount accountCl = mapper.findClientByUsername(username);
        if(accountCl == null) {
            throw new BussinessException(ExceptionType.bus_id_not_found,"用户不存在");
        }else{
            return accountCl;
        }
    }

    @Override
    public ClientAccount Login(ClientAccount clientAccount) {
        ClientAccount accountCl = mapper.findClientByUsername(clientAccount.getUserName());
        System.out.println(accountCl);
        if (accountCl == null) {
            throw new BussinessException(ExceptionType.bus_invalid_username,"username 错误！");
        }else{
            if (accountCl.getPassword().equals(clientAccount.getPassword())){
                return accountCl;
            }else{
                throw new BussinessException(ExceptionType.bus_invalid_password,"password 错误！");
            }
        }
    }


    @Transactional
    @Override
    public Integer updateClientPassword(ClientAccount clientAccount) {
        ClientAccount accountCl = mapper.findClientByUsername(clientAccount.getUserName());
        if (accountCl == null){
            throw new BussinessException(ExceptionType.bus_id_not_found,"用户不存在");
        }else {
            if (accountCl.getPhone().equals(clientAccount.getPhone())){
                accountCl.setPassword(clientAccount.getPassword());
                Integer res = mapper.updateClientPassword(accountCl);
                if (res!= 1){
                    throw new BussinessException(ExceptionType.bus_update_fail,"修改密码失败");
                }else {
                    return res;
                }
            }else{
                throw new BussinessException(ExceptionType.bus_invalid_phone,"phone 错误！");
            }

        }
    }
}
